/** Maximal Connencted Subgraph */
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> adjlist[30];
bool vis[30];
int dfs(int v) {
  if(vis[v]) return 0;
  vis[v] = true;
  for(auto el:adjlist[v]) {
    dfs(el);
  }
  return 1;
}
void solve() {
  n = cin.get() - 'A';
  cin.get();

  for(int i=0; i<n; ++i) {
    adjlist[i].clear();
    // adjlist[i].push_back(i);
  }

  string line;
  int cnt = 0;
  while(getline(cin,line)) { // NO '\n' in `line`
    if(line.size()==0)break;
    adjlist[line[0]-'A'].push_back(line[1]-'A');
    adjlist[line[1]-'A'].push_back(line[0]-'A');
  }

  for(int i=0; i<n; ++i) {
    cnt += dfs(i);
  }
  cout<<cnt<<"\n\n";
  return;
}
int main() {
  int tc;cin>>tc;cin.get();cin.get();
  for(int t=1;t<=tc;++t) {
    memset(vis, 0, sizeof(vis));
    solve();
  }
  return 0;
}
